package debtbundler.domain.entity.impl;


import static org.junit.Assert.*;

import org.junit.*;

import debtbundler.domain.entity.Debt;
import debtbundler.domain.entity.Payment;
import debtbundler.domain.entity.Person;
import debtbundler.domain.entity.Share;
import debtbundler.domain.entity.SharedCost;

public class MoneyPoolTest {
	
	@BeforeClass 
	public static void enableLogging() {
		debtbundler.logging.ConfigurationReader.setDefaultConf();
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}
	
	@Test
	public void testAddRemoveTransaction() {
		Person person = EntityManager.createPerson();
		SharedCost cost = EntityManager.createSharedCost();
		Payment payment = EntityManager.createPayment(person, cost); // EntityManager adds payment to person and shared cost
		
		person.removeTransaction(payment);
		assertFalse("Must be removed from both ends when removed from one", cost.getTransactions().contains(payment));
		
		cost.addPayment(payment);
		assertTrue("Must be added to both ends when added to one", cost.getTransactions().contains(payment));
	}
	
	@Test
	public void testGettingTransactions() {
		Person debtor = EntityManager.createPerson();
		SharedCost cost = EntityManager.createSharedCost();
		Payment payment = EntityManager.createPayment(debtor, cost); 
		Share share = EntityManager.createShare(debtor, cost);
		
		Person creditor = EntityManager.createPerson();
		Debt debt = EntityManager.createDebt(debtor, creditor);
		
		assertEquals(1, debtor.getInputTransactions().size());
		assertTrue(debtor.getInputTransactions().contains(share));
		assertEquals(2, debtor.getOutputTransactions().size());
		assertTrue(debtor.getOutputTransactions().contains(payment));
		assertTrue(debtor.getOutputTransactions().contains(debt));
		assertEquals(1, debtor.getShares().size());
		assertTrue(debtor.getShares().contains(share));
		assertEquals(1, debtor.getPayments().size());
		assertTrue(debtor.getPayments().contains(payment));
		assertEquals(1, debtor.getDebts().size());
		assertTrue(debtor.getDebts().contains(debt));
		
		assertEquals(1, cost.getInputTransactions().size());
		assertTrue(cost.getInputTransactions().contains(payment));
		assertEquals(1, cost.getOutputTransactions().size());
		assertTrue(cost.getOutputTransactions().contains(share));
		assertEquals(1, cost.getPayments().size());
		assertTrue(cost.getPayments().contains(payment));
		assertEquals(1, cost.getShares().size());
		assertTrue(cost.getShares().contains(share));
		
		assertEquals(1, creditor.getInputTransactions().size());
		assertTrue(creditor.getInputTransactions().contains(debt));
		assertEquals(0, creditor.getOutputTransactions().size());
	}

}
